﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
/// <summary>
/// 发货通知单操作插件
/// </summary>
/// <remarks>
/// 功能：审核后获交货数量占比、重量占比
/// 创建人：lxl
/// 创建日期：2024-1-8
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.YF.K3Cloud.Plugin
{
    [HotUpdate, Description("发货通知单操作插件")]
    public class SendNoticeOperatePlugin : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FQty");//销售数量
            e.FieldKeys.Add("FEntity");//单据体
            e.FieldKeys.Add("F_RDLV_AllQty");//整单数量
            e.FieldKeys.Add("F_RDLV_TotalWeigh");//整单重量
            e.FieldKeys.Add("F_RDLV_OutWeight");//行重量
            e.FieldKeys.Add("F_RDLV_WeightRate");//重量占比
            e.FieldKeys.Add("F_RDLV_QtyRate");//数量占比
        }
       

        /// <summary>
        /// 操作结束后功能处理
        /// </summary>
        /// <param name="e"></param>
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            //审核时执行:获取重量占比、交货数量占比
            if (FormOperation.OperationId == Kingdee.BOS.Core.Metadata.FormElement.FormOperation.Operation_Audit)
            {
                foreach (ExtendedDataEntity o in e.SelectedRows)
                {
                    //获取整单数量
                    decimal F_RDLV_AllQty = Convert.ToDecimal(o["F_RDLV_AllQty"]);
                    //获取整单重量
                    decimal F_RDLV_TotalWeigh = Convert.ToDecimal(o["F_RDLV_TotalWeigh"]);
                    //交货数量占比
                    decimal fQtyRateLeft = 1;
                    //重量占比
                    decimal fWeightRateLeft = 1;
                    DynamicObjectCollection fEntity = o["SAL_DELIVERYNOTICEENTRY"] as DynamicObjectCollection;
                    foreach (DynamicObject o2 in fEntity)
                    {
                        //获取当前行的索引
                        int fIndex= fEntity.IndexOf(o2);
                        //获取销售数量
                        decimal FQty = Convert.ToDecimal(o2["Qty"]);
                        if(F_RDLV_AllQty>0)
                        {
                            decimal F_RDLV_QtyRate=0;
                            //前面都按照重量计算
                            if (fIndex< fEntity.Count-1)
                            {
                                //计算交货数量占比
                                 F_RDLV_QtyRate = Math.Round(FQty / F_RDLV_AllQty, 2);
                                //扣减占比
                                fQtyRateLeft = fQtyRateLeft - F_RDLV_QtyRate;
                            }else//最后一行倒挤出占比
                            {
                                F_RDLV_QtyRate = fQtyRateLeft;
                            }
                            o2["F_RDLV_QtyRate"] = F_RDLV_QtyRate;
                        }
                       
                        //获取重量
                        decimal F_RDLV_OutWeight = Convert.ToDecimal(o2["F_RDLV_OutWeight"]);
                        decimal F_RDLV_WeightRate;
                        if (F_RDLV_TotalWeigh>0)
                        {
                            //前面都按照重量计算
                            if (fIndex < fEntity.Count - 1)
                            {
                                //计算交货数量占比
                                F_RDLV_WeightRate = Math.Round(F_RDLV_OutWeight / F_RDLV_TotalWeigh, 2);
                                //扣减占比
                                fWeightRateLeft = fWeightRateLeft - F_RDLV_WeightRate;
                            }
                            else//最后一行倒挤出占比
                            {
                                F_RDLV_WeightRate = fWeightRateLeft;
                            }
                            o2["F_RDLV_WeightRate"] = F_RDLV_WeightRate;
                        }

                    }


                }
            }

            
        }

    }
}
